package com.yuanfeng.job.job;


import com.alibaba.fastjson.JSON;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.base.MemberMessageListIntegralDTO;
import com.yuanfeng.commoms.rocketmq.MqSendUtils;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.fx.GoodsVkSetVO;
import com.yuanfeng.job.feign.BaseServiceClient;
import com.yuanfeng.job.feign.UserServiceClient;
import com.yuanfeng.job.mapper.ActivityNoneListMapper;
import com.yuanfeng.job.mapper.OrderConsignerMapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UpdateVirtualOrderTimeJob
 * @Description 修改虚拟订单的过期时间
 * @Author liulh
 * @Date 2023/5/17 14:33
 * @Version 1.0
 **/
public class UpdateVirtualOrderTimeJob implements BaseJob {

    @Autowired
    private OrderConsignerMapper orderConsignerMapper;

    @Autowired
    private ActivityNoneListMapper activityNoneListMapper;

    @Autowired
    private BaseServiceClient baseServiceClient;

    @Autowired
    private UserServiceClient userServiceClient;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        ResponseResult result = baseServiceClient.queryGoodsVkSet();
        GoodsVkSetVO goodsVkSet = JSON.parseObject(JSON.toJSONString(result.getData()), GoodsVkSetVO.class);
        //修改虚拟商品过期订单
        List<String> orders = orderConsignerMapper.queryVirtualOrderTime();
        if(null != orders && orders.size()>0){
            for (String order : orders) {
                orderConsignerMapper.upVirtualOrder(order);
                //判断是否是分销订单
                Integer num = orderConsignerMapper.selectByPrimaryKey(order);
                if (null != num) {
                    orderConsignerMapper.updateOrderAfterSaleTime(order, DateUtils.getAfterDate(goodsVkSet.getAfterSalesDays()));
                }
                //增加积分
                ResponseResult resultResp = baseServiceClient.queryIntegralRules();
                Map<String, Integer> integralRules = (Map<String, Integer>) resultResp.getData();
                Map<String,Object> map = orderConsignerMapper.selectUserMoneyByOrderId(order);
                BigDecimal money= (BigDecimal) map.get("orderPaymentAmount");
                //修改会员积分和会员等级
                MemberMessageListIntegralDTO updateMemberMessageListIntegral = new MemberMessageListIntegralDTO();
                BigDecimal jifen = money.multiply(new BigDecimal(integralRules.get("ruleConsume")));
                long l = jifen.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
                ArrayList<String> list = new ArrayList<>();
                //当前逻辑存在问题，需要将积分加到员工账号上
//                    if (ui.getIsEmployee() == 1) {
//                        //当是员工账号是积分归属企业主账号
//                        userId = userServiceClient.getPrimaryAccountId(userId);
//                    }
                list.add(map.get("buyerUserId").toString());
                updateMemberMessageListIntegral.setUserIdList(list);
                updateMemberMessageListIntegral.setIntegral((int) l);
                //1是增加积分 0 是扣除积分
                updateMemberMessageListIntegral.setType(CommonType.commonStatus.YES.getCode());
                userServiceClient.updateMemberIntegral(updateMemberMessageListIntegral);
                //添加积分修改记录
                Map<String, Object> pointParam = new HashMap<>();
                pointParam.put("PointsLogType", CommonType.pointsLogType.GAIN.getCode());
                pointParam.put("ClassId", CommonType.pointType.PURCHASE.getCode());
                pointParam.put("PointsLogTime", DateUtils.getTime());
                pointParam.put("PointsLogDesc", CommonType.pointType.PURCHASE.getDesc());
                pointParam.put("PointsLogFlag", "reg");
                pointParam.put("UserId",map.get("buyerUserId").toString());
                pointParam.put("orderId",order);
                pointParam.put("PointsLogPoints", (int) l);
                pointParam.put("PointsLogOperate", CommonType.pointType.PURCHASE.getDesc());
                pointParam.put("proportion", integralRules.get("ruleConsume"));
                userServiceClient.insertPointLogByMap(pointParam);
            }
        }
        //下架过期虚拟商品
        List<String> commonIds = activityNoneListMapper.queryGuoQiVirtualGoods();
        if(null != commonIds && commonIds.size() > 0){
            for (String commonId : commonIds) {
                activityNoneListMapper.updateGoodsStart(commonId);
            }
            Map<String,Object> map= new HashMap<>();
            // 1 是添加  2是删除    屏蔽
            map.put("type",2);
            //通过mq 更新es的商品数据
            map.put("commonIdS",commonIds);
            MqSendUtils.mqSendGoods(map);
        }
    }
}
